* **************************************************************************** *
* Sierra Leone - Social Signaling and Childhood Immunization                   *
* Table: Test of the Equality of Distance Coefficients                         *
* **************************************************************************** *
/*
** Purpose:    Create Tables on Externalities:
               Test of the Equality of Distance Coefficients from Table
			   

*/
********************************************************************************
********************************************************************************



	use  "${Replicate_SocialSignals_dtaInter}/Endline_Distance_Analysis.dta", clear


	* Create and label the distance variables:

	rename  newdist distance
	replace distance = 0         if distance<=0.5
	replace distance = 1         if distance>0.5&distance<=1.5
	replace distance = 2         if distance>1.5&distance<=2.5
	replace distance = 3         if distance>2.5&distance<=3.5
	replace distance = 4         if distance>3.5&distance<=4.5
	replace distance = 5         if distance>4.5&distance!=.

	tab     distance, gen(dist)
	lab var dist2 "1 mile"
	lab var dist3 "2 miles"
	lab var dist4 "3 miles"
	lab var dist5 "4 miles"
	lab var dist6 "5 miles"

	summ    dist?


	* Label outcome variables:
	lab var  penta2    "3 Vaccines"
	lab var  penta3    "4 Vaccines"
	lab var  measles1  "5 Vaccines"


	** Code timely vaccine outcomes
	local a = 91

	foreach    var in bcg {
		replace `var' = 0         if `var'==1&age_`var'>=`a'&age_`var'!=.
		replace `var' = 0         if `var'==.&age_pikin>=`a'&age_pikin!=.
		replace `var' = .         if age_pikin<`a'
	}

	local b = 122

	foreach var in penta1 {
		replace `var' = 0         if `var'==1&age_`var'>=`b'&age_`var'!=.
		replace `var' = 0         if `var'==.&age_pikin>=`b'&age_pikin!=.
		replace `var' = .         if age_pikin<`b'

		replace `var' = 0         if bcg ==0&`var'==1
	}

	local c = 152

	foreach  var in penta2 {
		replace `var' = 0         if `var'==1&age_`var'>=`c'&age_`var'!=.
		replace `var' = 0         if `var'==.&age_pikin>=`c'&age_pikin!=.
		replace `var' = .         if age_pikin<`c'

		replace `var' = 0         if penta1 ==0&`var'==1
	}

	local d = 183

	foreach  var in penta3 {
		replace `var' = 0         if `var'==1&age_`var'>=`d'&age_`var'!=.
		replace `var' = 0         if `var'==.&age_pikin>=`d'&age_pikin!=. 
		replace `var' = .         if age_pikin<`d'

		replace `var' = 0         if penta2 ==0&`var'==1
	}


	local e = 350

	foreach var in measles1 {
		replace `var' = 0         if `var'==1&age_`var'>=`e'&age_`var'!=.
		replace `var' = 0         if `var'==.&age_pikin>=`e'&age_pikin!=.
		replace `var' = .         if age_pikin<`e'

		replace `var' = 0         if penta3 ==0&`var'==1
	}


	foreach    var in age_pikin mother_age floor roof schooling farm trading birth_order {
		summ    `var', detail
		replace `var' = `r(mean)'    if `var'==.
	}


	* --------------------------------------------------------------------------
	* Run regressions and store results:

	foreach    var in measles1 penta3 penta2   {
		reg     `var' treat2 treat3 treat4 dist2 dist3 dist4 dist5 dist6
		est store `var'_reg1
	}

	foreach    var in measles1 penta3 penta2   {
		reg     `var' treat2 treat3 treat4 dist2 dist3 dist4 dist5 dist6 mother_age floor roof schooling farm trading birth_order
		est store `var'_reg2
	}

	* --------------------------------------------------------------------------
	* Generate table:

	eststo clear
	foreach var in measles1 {
		foreach num of numlist 2 3 4 5 6  {
			suest  `var'_reg1 `var'_reg2, vce(cluster clinic)
			nlcom   measles1: [`var'_reg1_mean]dist`num' - [`var'_reg2_mean]dist`num', post
			estadd  local Obs            = "`e(N)'"
			eststo  model`var'`num'
		}
	}
	esttab  ///
	modelmeasles12 modelmeasles13 modelmeasles14                        ///
	modelmeasles15 modelmeasles16                                       ///
	using "${Replicate_SocialSignals_ATables}/Table_EqualityDistanceTest_raw.tex",             ///
	prehead("\begin{tabular}{l*{6}{c}} \toprule"                        ///
	"& \multicolumn{1}{c}{\textbf{Distance 1 mile}} & \multicolumn{1}{c}{\textbf{2 miles}} & \multicolumn{1}{c}{\textbf{3 miles}} & \multicolumn{1}{c}{\textbf{4 miles}} & \multicolumn{1}{c}{\textbf{5 miles}} \\" ///
	"& \multicolumn{1}{c}{(1)}                      & \multicolumn{1}{c}{(2)}              & \multicolumn{1}{c}{(3)}              & \multicolumn{1}{c}{(4)}              & \multicolumn{1}{c}{(5)} \\ \hline") ///
	sfmt(3) b(3) se(3)  ${StarsOpt}                                     ///
	scalars("Obs       Observations")                                   ///
	fragment nogaps nomtitles label noobs  nonumbers  nolines           ///
	replace

	foreach var in penta3  {
		foreach num of numlist 2 3 4 5 6  {
			suest  `var'_reg1 `var'_reg2, vce(cluster clinic)
			nlcom   penta3: [`var'_reg1_mean]dist`num' - [`var'_reg2_mean]dist`num', post
			estadd  local Obs            = "`e(N)'"
			eststo  model`var'`num'
		}
	}
	esttab  ///
	modelpenta32 modelpenta33 modelpenta34 modelpenta35 modelpenta36    ///
	using "${Replicate_SocialSignals_ATables}/Table_EqualityDistanceTest_raw.tex",           ///
	scalars("Obs       Observations")                                   ///
	sfmt(3) b(3) se(3)  ${StarsOpt}                                     ///
	fragment append nomtitles noobs nonumbers  label nolines            ///

	foreach var in penta2  {
		foreach num of numlist 2 3 4 5 6  {
			suest  `var'_reg1 `var'_reg2, vce(cluster clinic)
			nlcom   penta2: [`var'_reg1_mean]dist`num' - [`var'_reg2_mean]dist`num', post
			estadd  local Obs            = "`e(N)'"
			eststo  model`var'`num'
		}
	}

	esttab ///
	modelpenta22 modelpenta23 modelpenta24 modelpenta25 modelpenta26    ///
	using "${Replicate_SocialSignals_ATables}/Table_EqualityDistanceTest_raw.tex",             ///
	scalars("Obs       Observations")                                   ///
	sfmt(3) b(3) se(3)  ${StarsOpt}                                     ///
	fragment append nomtitles noobs nonumbers  nolines                  ///
	postfoot("\bottomrule \end{tabular}")                               ///
	label


	filefilter "${Replicate_SocialSignals_ATables}/Table_EqualityDistanceTest_raw.tex"      ///
	 "${Replicate_SocialSignals_ATables}/Table_EqualityDistanceTest.tex", from("[1em]") to(" ") replace
	erase	     "${Replicate_SocialSignals_ATables}/Table_EqualityDistanceTest_raw.tex"


* **************************************************************************** *
* End of Do file !!!
* **************************************************************************** *
